package com.dm.library.widgets.wheelview.common;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.dm.library.R;

import java.util.List;

/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  时间，地址选择弹框
 * Created by tangjian on 2016/11/22.
 */

public class WheelViewPopupWindow extends PopupWindow {
    private View view;
    private CommonUseWheelView mCommonUseWheelView;
    private OnViewClickListener mOnViewClickListener;

    public void setOnViewClickListener(OnViewClickListener onViewClickListener) {
        this.mOnViewClickListener = onViewClickListener;
    }

    public WheelViewPopupWindow(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.wheel_view_dialog, null);
        mCommonUseWheelView = (CommonUseWheelView) view.findViewById(R.id.common_wheel_view);
        mCommonUseWheelView.setOnClickListener(new CommonUseWheelView.OnClickListener() {
            @Override
            public void onCancelClick() {
                dismiss();
            }

            @Override
            public void onDownClick(String firstItem, String secondItem, String thirdItem, String fourthItem) {
                dismiss();
                if (null != mOnViewClickListener) {
                    mOnViewClickListener.onCompleteClick(firstItem, secondItem, thirdItem, fourthItem);
                }
            }
        });

        view.findViewById(R.id.top_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        this.setFocusable(true);
        this.setOutsideTouchable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        this.setBackgroundDrawable(dw);
        // 设置popWindow的显示和消失动画
        this.setAnimationStyle(R.style.AnimBottom);
    }

    public void show(View view) {
        // 在底部显示
        showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    public void initTimeDatas(boolean isShowHour) {
        mCommonUseWheelView.initTimeDatas(isShowHour);
        mCommonUseWheelView.setWheelViewVisibility(true, true, false);
    }

    public void setAreaDatas(List<ProvinceInfo> provinceInfos) {
        if (null != provinceInfos) {
            mCommonUseWheelView.setAreaDatas(provinceInfos, true);
            mCommonUseWheelView.setWheelViewVisibility(true, true, false);
        } else {
            throw new IllegalArgumentException("数据集合不能为空");
        }
    }

    public void setCommonDatas(List<String> commonDatas) {
        if (null != commonDatas) {
            mCommonUseWheelView.setCommonDatas(commonDatas);
            mCommonUseWheelView.setWheelViewVisibility(false, false, false);
        } else {
            throw new IllegalArgumentException("数据集合不能为空");
        }
    }

    public interface OnViewClickListener {
        /**
         * 完成
         */
        void onCompleteClick(String first, String second, String third, String fourth);
    }
}
